<?php

namespace app\manage\services;
use app\model\Role as RoleModel;
use app\model\Auth as AuthModel;

class RoleAuth
{
    protected $roleModel;
    protected $authModel;

    public function __construct()
    {
        $this->roleModel = new RoleModel();
        $this->authModel = new AuthModel();
    }

    //查询角色分页数据
    public function getPageList($page, $limit, $where = []): array
    {
        return $this->roleModel->where($where)
            ->order("id desc")
            ->paginate(['page' => $page, 'list_rows' => $limit])
            ->toArray();
    }

    //查询权限所有数据
    public function getAuthList(): array
    {
        return formatAuth($this->authModel->select()->toArray());
    }

    //根据id查询角色明细
    public function getInfoById($id)
    {
        $info =$this->roleModel->where('id', $id)->find();
        $info['auth'] = $this->authModel->where('id','in',$info['auth_ids'])->select()->toArray();
        $info['auth'] = formatAuth($info['auth']);
        return $info;
    }

    //新增角色
    public function add($params)
    {
        return $this->roleModel->createInfo($params);
    }

    //编辑角色
    public function update($params): bool
    {
        return $this->roleModel->updateInfo($params['id'], $params);
    }

    //删除角色
    public function delete($id): bool
    {
        $info = $this->roleModel->find($id);
        $res = $info->delete();
        if ($res) {
            return true;
        }
        return false;
    }

    //给账号设置角色
    public function setting($params): bool
    {
        return $this->roleModel->updateInfo($params['id'], $params);
    }
}